package com.yitong.equipment.ext

import android.os.Build
import android.os.Build.VERSION
import android.text.TextUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.SPUtils
import com.yitong.equipment.constant.AppConstant
import com.yitong.equipment.constant.HttpConstant
import com.yitong.equipment.constant.UrlConstant
import com.yitong.equipment.entity.other.InterfaceConfigBean
import com.yitong.equipment.network.oss.OssConfig

fun initConfig(appId: String, versionCode: Long, versionName: String) {
    SPUtils.getInstance().put(HttpConstant.Lib_APPLICATION_ID, appId)
    SPUtils.getInstance().put(HttpConstant.Lib_VERSION_CODE, versionCode)
    SPUtils.getInstance().put(HttpConstant.Lib_VERSION_NAME, versionName)
    SPUtils.getInstance().put(HttpConstant.Lib_SettingVersion, versionName)

    var ua = ""
    var webUa = ""

    ua = getDeviceAgent() + " YiTongAI/" + versionName
    webUa = " HybirdApp ytai/$versionName"
    SPUtils.getInstance().put(HttpConstant.Lib_UserAgent, ua)
    SPUtils.getInstance().put(HttpConstant.Lib_WebViewUserAgent, webUa)
    SPUtils.getInstance().put(HttpConstant.Lib_CONFIG_HOST, UrlConstant.AppConfigUrl)
}


fun getDeviceAgent(): String {
    var uniqueDeviceId = DeviceUtils.getUniqueDeviceId()
    if (uniqueDeviceId == null || TextUtils.isEmpty(uniqueDeviceId)) {
        uniqueDeviceId = "unknown"
    }
    return " (Android ${VERSION.RELEASE}; ${Build.BRAND}; ${Build.MODEL}; ${uniqueDeviceId})"
}


/**
 * 拼接接口配置请求地址
 */
fun getInterfaceConfig(): String {
    return SPUtils.getInstance().getString(HttpConstant.Lib_CONFIG_HOST, "") + SPUtils.getInstance()
        .getString(HttpConstant.Lib_VERSION_NAME, "") + "%2B" + SPUtils.getInstance()
        .getLong(HttpConstant.Lib_VERSION_CODE)
}

fun saveAppConfig(interfaceConfigBean: InterfaceConfigBean) {
    SPUtils.getInstance().put(HttpConstant.LIB_channel, interfaceConfigBean.channel)
    SPUtils.getInstance().put(HttpConstant.LIB_BaseHost, interfaceConfigBean.data?.ApiBase)
    SPUtils.getInstance().put(HttpConstant.LIB_AppStoreUrl, interfaceConfigBean.data?.AppStoreUrl)
    SPUtils.getInstance().put(HttpConstant.LIB_WebTraceBase, interfaceConfigBean.data?.WebTraceBase)
    SPUtils.getInstance().put(HttpConstant.LIB_EipApiBase, interfaceConfigBean.data?.EipApiBase)
    SPUtils.getInstance()
        .put(HttpConstant.LIB_TrainingInitialMonth, interfaceConfigBean.data?.TrainingInitialMonth)
    interfaceConfigBean.data?.VideoCompletionProgress?.let {
        SPUtils.getInstance().put(HttpConstant.LIB_VideoCompletionProgress, it)
    }
    interfaceConfigBean.data?.VideoStudyProgress?.let {
        SPUtils.getInstance().put(HttpConstant.LIB_VideoStudyProgress, it)
    }
    interfaceConfigBean.data?.LiveCompletionSeconds?.let {
        SPUtils.getInstance().put(HttpConstant.LIB_LiveCompletionSeconds, it)
    }

    when (interfaceConfigBean.channel) {
        "Prod" -> SPUtils.getInstance()
            .put(HttpConstant.Lib_APP_KEY, AppConstant.ProdKey)
        "Dev" -> SPUtils.getInstance()
            .put(HttpConstant.Lib_APP_KEY, AppConstant.DevKey)
        "Test" -> SPUtils.getInstance()
            .put(HttpConstant.Lib_APP_KEY, AppConstant.TestKey)
    }

    var OSSBase = interfaceConfigBean.data?.OSSBase?.let { interfaceConfigBean.data?.OSSBase } ?: ""
    var OSSBucketName =
        interfaceConfigBean.data?.OSSBucketName?.let { interfaceConfigBean.data?.OSSBucketName }
            ?: ""
    var OSSEndpoint =
        interfaceConfigBean.data?.OSSEndpoint?.let { interfaceConfigBean.data?.OSSEndpoint } ?: ""
    var OSSPrefix =
        interfaceConfigBean.data?.OSSPrefix?.let { interfaceConfigBean.data?.OSSPrefix } ?: ""
    var OSSSignatureEndpoint =
        interfaceConfigBean.data?.OSSSignatureEndpoint?.let { interfaceConfigBean.data?.OSSSignatureEndpoint }
            ?: ""

    OssConfig(OSSBase, OSSBucketName, OSSEndpoint, OSSPrefix, OSSSignatureEndpoint)
}

/**
 * 获取请求根地址
 */
fun getBaseHost(): String {
    return SPUtils.getInstance()
        .getString(HttpConstant.LIB_BaseHost, "http://ytai-api.ytzxjy.com/")
}



